var brandList, pbcList;

$(function () {

	var re = /[\u4e00-\u9fa5a-zA-Z\d]/;


    //产品弹出框 点击结果
    $('#md_new_lgsNode').on('show.bs.modal', function (event) {
        $.get("/getBiztype" , function (re) {
            var list = re.data;
            var str="";
            if(list.length > 0){
                for(var i in list){
                    str += "<input type='checkbox' value='"+list[i].id+"' name='biztype'/>"+list[i].typename +"&nbsp;&nbsp;";
                    if((i+1)%5 == 0){
                        str += "<br/>";
                    }

                }
            }
            $("#new_biztype").html(str);
        });
    });
    //新增节点
    $("[name='btn_save_new_lgsNode']").click(function () {
        var form = window.form_new_lgsNode;
        
        var name = form.name.value;
        var type = form.type.value;
        var sta = form.sta.value;
        var biztypes = "";
        $('input[name="biztype"]:checked').each(function(){
            biztypes += $(this).val()+",";
        })

        if (!name) {
            bootbox.balert("节点名称不能为空 !");
            return;
        }
        if (!re.test(name)) {
        	bootbox.balert("节点名称仅限汉字、数字和字母 !");
            return;
        }
        if (!type) {
        	bootbox.balert("请选择流向类型 !");
            return;
        }
       
        var lgsNode = {name: name, type: type, sta:sta, biztypes:biztypes};
        $.postJsonBody("/lgsNode", lgsNode, function (re) {
        	window.location.href=window.location.href; 
        	window.location.reload; 
        })
    });

    //编辑保存节点
    $("[name='btn_save_edit_lgsNode']").click(function () {
        var form = window.form_edit_lgsNode;
        var id = form.id.value;
        var name = form.name.value;
        var sta = form.sta.value;
        var biztypes = "";
        $('input[name="editBiztype"]:checked').each(function(){
            biztypes += $(this).val()+",";
        })
        if (name == null || name == '') {
            bootbox.balert("节点名称不能为空");
            return;
        }

        var lgsNode = {id: id, name: name, sta: sta, biztypes:biztypes};
        $.putJsonBody("/lgsNode/"+id, lgsNode, function (re) {
            // tipper.success("已保存，请刷新页面");
            // $("#md_new_brand").modal("hide");
        	window.location.href = window.location.href; 
        	window.location.reload; 
        })
    });

    
});  


// 节点详情
$('#md_edit_lgsNode').on('show.bs.modal', function (event) {
    var _e = $(event.relatedTarget);
    var id = _e.data("lgsnode-id");
    var modal = $(this);
    var form = window.form_edit_lgsNode;
    $.get("/lgsNode/" + id, function (re) {
        var lgsNode = re.data;
        form.id.value = lgsNode.id;
        form.name.value = lgsNode.name;
        form.type.value = lgsNode.type;
        $(form.sta).val(lgsNode.sta);
    });

    //获取节点关联的 ERP适用单据
    $.get("/getBiztypeByEeit?nodeId="+id , function (re) {
        var list = re.data.list,secList=re.data.secList;
        var str="";
        var a=list.length, b=secList.length,c=a+b,d=c;

        if(b > 0){
            for(var i=0;i<b; i++){
                str += "<input type='checkbox' value='"+secList[i].id+"' name='editBiztype' checked='true'/>"+secList[i].typename +"&nbsp;&nbsp;";
                d = d-1;
                if((c-d)%5 == 0){
                    str += "<br/>";
                }
            }
        }
        if(a > 0){
            for(var i=0;i<a; i++){
                str += "<input type='checkbox' value='"+list[i].id+"' name='editBiztype'/>"+list[i].typename +"&nbsp;&nbsp;";
                d = d-1;
                if((c-d)%5 == 0){
                    str += "<br/>";
                }
            }
        }
        $("#edit_biztype").html(str);
    });
    //获取可选 ERP适用单据
});

//查询条件重置
$("#search_reset").click(function(){
	$("input").val("");
    $("select").val("");
});


